package com.dlwx.parttimetalent.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.media.MediaMetadataRetriever;
import android.util.Log;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Administrator on 2017/12/20.
 */

public class WaterMarkUtil {

    public static File  addWater(File file, Context contextCompat){
        MediaMetadataRetriever mmr=new MediaMetadataRetriever();//实例化MediaMetadataRetriever对象
//        File file=new File(path);//实例化File对象，文件路径为/storage/sdcard/Movies/music1.mp4
        mmr.setDataSource(file.getAbsolutePath());//设置数据源为该文件对象指定的绝对路径
        Bitmap bitmap=mmr.getFrameAtTime();//获得视频第一帧的Bitmap对象
//        Bitmap watermark = BitmapFactory.decodeResource(contextCompat.getResources(), R.mipmap.media_player);
//        Bitmap targetBitmap=createWaterMaskCenter(bitmap,watermark);
        return saveBitmap(bitmap, contextCompat);
    }


    /**
     * 设置水印图片到中间

     * @param src
     * @param watermark
     * @return
     */
    public static Bitmap createWaterMaskCenter(Bitmap src, Bitmap watermark) {
        return createWaterMaskBitmap(src, watermark,
                (src.getWidth() - watermark.getWidth()) / 2,
                (src.getHeight() - watermark.getHeight()) / 2);
    }

    private static Bitmap createWaterMaskBitmap(Bitmap src, Bitmap watermark, int paddingLeft, int paddingTop) {
        if (src == null) {
            return null;
        }
        int width = src.getWidth();
        int height = src.getHeight();
        //创建一个bitmap
        Bitmap newb = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
        //将该图片作为画布
        Canvas canvas = new Canvas(newb);
        //在画布 0，0坐标上开始绘制原始图片
        canvas.drawBitmap(src, 0, 0, null);
        //在画布上绘制水印图片
        canvas.drawBitmap(watermark, paddingLeft, paddingTop, null);
        // 保存
        canvas.save(Canvas.ALL_SAVE_FLAG);
        // 存储
        canvas.restore();
        return newb;
    }


    /** 保存方法
     * @param targetBitmap
     * @param */
    public static File   saveBitmap(Bitmap targetBitmap, Context context) {
        Log.e("CCC", "保存图片");
        File f = new File( SDCardUtils.getApplicationPath(context), System.currentTimeMillis()+".png");
        if (f.exists()) {
            f.delete();
        }
        try {
            f.createNewFile();
            FileOutputStream out = new FileOutputStream(f);
            targetBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
            Log.i("CCC", f.exists()+"已经保存"+f.getAbsolutePath());
        } catch (FileNotFoundException e) {
            Log.i("CCC", "失败"+e.toString());
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.i("CCC", "失败"+e.toString());
            e.printStackTrace();
        }
        return f;

    }

}
